cmake_minimum_required(VERSION 2.8)

PROJECT(libsynctory C)

set (CMAKE_MODULE_PATH ${libsynctory_SOURCE_DIR}/CMake)

file(READ ${libsynctory_SOURCE_DIR}/src/include/libsynctory/version.h LIBSYNCTORY_VERSION_H_CONTENTS)
string(REGEX MATCH "LIBSYNCTORY_VERSION_FLAVOUR[ \t]+\"([A-Z]+)\"" LIBSYNCTORY_VERSION_FL ${LIBSYNCTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "(CURRENT|STABLE|ALPHA|BETA|PRERELEASE|RELEASE)" LIBSYNCTORY_VERSION_FL ${LIBSYNCTORY_VERSION_FL})
string(REGEX MATCH "LIBSYNCTORY_VERSION_MAJOR[ \t]+([0-9]+)" LIBSYNCTORY_VERSION_MJ ${LIBSYNCTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" LIBSYNCTORY_VERSION_MJ ${LIBSYNCTORY_VERSION_MJ})
string(REGEX MATCH "LIBSYNCTORY_VERSION_MINOR[ \t]+([0-9]+)" LIBSYNCTORY_VERSION_MI ${LIBSYNCTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" LIBSYNCTORY_VERSION_MI ${LIBSYNCTORY_VERSION_MI})
string(REGEX MATCH "LIBSYNCTORY_VERSION_PATCH[ \t]+([0-9]+)" LIBSYNCTORY_VERSION_PT ${LIBSYNCTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" LIBSYNCTORY_VERSION_PT ${LIBSYNCTORY_VERSION_PT})
string(REGEX MATCH "LIBSYNCTORY_VERSION_SUFFX[ \t]+([0-9]+)" LIBSYNCTORY_VERSION_SX ${LIBSYNCTORY_VERSION_H_CONTENTS})
string(REGEX MATCH "([0-9]+)" LIBSYNCTORY_VERSION_SX ${LIBSYNCTORY_VERSION_SX})

string(COMPARE EQUAL ${LIBSYNCTORY_VERSION_FL} "RELEASE" LIBSYNCTORY_RELEASE)
if(LIBSYNCTORY_RELEASE)
	set(LIBSYNCTORY_VERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI}.${LIBSYNCTORY_VERSION_PT})
	set(LIBSYNCTORY_LIBVERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI})
else(LIBSYNCTORY_RELEASE)
	string(COMPARE EQUAL ${LIBSYNCTORY_VERSION_FL} "PRERELEASE" LIBSYNCTORY_PRERELEASE)
	if(LIBSYNCTORY_PRERELEASE)
		set(LIBSYNCTORY_VERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI}.${LIBSYNCTORY_VERSION_PT}-rc${LIBSYNCTORY_VERSION_SX})
		set(LIBSYNCTORY_LIBVERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI}-rc${LIBSYNCTORY_VERSION_SX})
	else(LIBSYNCTORY_PRERELEASE)
		string(COMPARE EQUAL ${LIBSYNCTORY_VERSION_FL} "BETA" LIBSYNCTORY_BETA)
		if(LIBSYNCTORY_BETA)
			set(LIBSYNCTORY_VERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI}.${LIBSYNCTORY_VERSION_PT}-b${LIBSYNCTORY_VERSION_SX})
			set(LIBSYNCTORY_LIBVERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI}-b${LIBSYNCTORY_VERSION_SX})
		else(LIBSYNCTORY_BETA)
			string(COMPARE EQUAL ${LIBSYNCTORY_VERSION_FL} "ALPHA" LIBSYNCTORY_ALPHA)
			if(LIBSYNCTORY_ALPHA)
				set(LIBSYNCTORY_VERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI}.${LIBSYNCTORY_VERSION_PT}-a${LIBSYNCTORY_VERSION_SX})
				set(LIBSYNCTORY_LIBVERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI}-a${LIBSYNCTORY_VERSION_SX})
			else(LIBSYNCTORY_ALPHA)
				string(COMPARE EQUAL ${LIBSYNCTORY_VERSION_FL} "STABLE" LIBSYNCTORY_STABLE)
				if(LIBSYNCTORY_STABLE)
					set(LIBSYNCTORY_VERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI}-STABLE)
					set(LIBSYNCTORY_LIBVERSION ${LIBSYNCTORY_VERSION_MJ}.${LIBSYNCTORY_VERSION_MI}-STABLE)
				else(LIBSYNCTORY_STABLE)
					set(LIBSYNCTORY_VERSION ${LIBSYNCTORY_VERSION_MJ}.0-CURRENT)
					set(LIBSYNCTORY_LIBVERSION ${LIBSYNCTORY_VERSION_MJ}.0-CURRENT)
				endif(LIBSYNCTORY_STABLE)
			endif(LIBSYNCTORY_ALPHA)
		endif(LIBSYNCTORY_BETA)
	endif(LIBSYNCTORY_PRERELEASE)
endif(LIBSYNCTORY_RELEASE)

set(PACKAGE "libsynctory")
set(PACKAGE_NAME ${PACKAGE})
set(PACKAGE_VERSION ${LIBSYNCTORY_VERSION})

set(CPACK_GENERATOR "TBZ2")
set(CPACK_SOURCE_GENERATOR "TBZ2")
set(CPACK_PACKAGE_VERSION ${PACKAGE_VERSION})
set(CPACK_SOURCE_IGNORE_FILES "/build/;libsynctory.kdev4;.kdev4/;.*~")

set(LIBSYNCTORY_LIBS "")
set(CMAKE_C_FLAGS "-std=c99 -pedantic -Wall -Wextra -pipe -O3 -fno-strict-aliasing")
set(CMAKE_REQUIRED_FLAGS ${CMAKE_ANSI_CFLAGS})
set(CMAKE_REQUIRED_DEFINITIONS -D_FILE_OFFSET_BITS=64)
set(CMAKE_REQUIRED_LIBRARIES ${LIBSYNCTORY_LIBS})

add_subdirectory(src)
add_subdirectory(doc)
include(CPack)